<?php

/**
 * This is the model class for table "{{attachment}}".
 *
 * The followings are the available columns in table '{{attachment}}':
 * @property integer $id
 * @property integer $profile_id
 * @property string $name
 * @property string $file_name
 * @property string $create_date
 * @property string $created_by
 * @property string $update_date
 * @property string $updated_by
 * 
 * The followings are the available model relations:
 * @property Profile $profile
 */
class Attachment extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Attachment the static model class
	 */
	public $keyword;
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{attachment}}';
	}
	
	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array ('profile_id, name, file_name','required','message' => t ( 'MSG_REQUIRED', 'messages' )),
			array('profile_id', 'numerical', 'integerOnly'=>true, 'message' => t ( 'MSG_WRONG_NUMBER', 'messages' )),
			array('name, file_name', 'length', 'max'=>100, 'tooLong' => t ( 'MSG_STRING_TOO_LONG', 'messages' )),
			array('created_by, updated_by', 'length', 'max'=>45, 'tooLong' => t ( 'MSG_STRING_TOO_LONG', 'messages' )),
			array('file_name', 'file', 'types' => 'JPG,jpg,jpeg,bmp,png,gif,doc,docx,xls,xlsx,pdf,PDF,mp3,mp4', 'maxSize' => 1024 * 1024 * 5, // 5MB
				'tooLarge' => t('MSG_IMAGE_TOO_LARGE', 'messages'),
				'wrongType' => t('MSG_FILE_WRONG_TYPE', 'messages'),
				'allowEmpty' => false, 'on' => 'create'),
			array('file_name', 'length', 'max' => 100, 'tooLong' => t('MSG_STRING_TOO_LONG', 'messages'), 'on' => 'create'),
			array('file_name', 'file', 'types' => 'JPG,jpg,jpeg,bmp,png,gif,doc,docx,xls,xlsx,pdf,PDF,mp3,mp4', 'maxSize' => 1024 * 1024 * 5, // 5MB
					'tooLarge' => t('MSG_IMAGE_TOO_LARGE', 'messages'),
					'wrongType' => t('MSG_FILE_WRONG_TYPE', 'messages'),
					'allowEmpty' => true, 'on' => 'update'),
			array('create_date, update_date', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('keyword,profile_id, id, name, file_name, create_date, created_by, update_date, updated_by', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'profile' => array(self::BELONGS_TO, 'Profile', 'profile_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => t('LBL_ID', 'attachment'),
			'profile_id' => t('LBL_PROFILE', 'attachment'),
			'name' => t('LBL_NAME', 'attachment'),
			'file_name' => t('LBL_FILE_NAME', 'attachment'),
			'create_date' => t('LBL_CREATE_DATE', 'attachment'),
			'created_by' => t('LBL_CREATED_BY', 'attachment'),
			'update_date' => t('LBL_UPDATE_DATE', 'attachment'),
			'updated_by' => t('LBL_UPDATED_BY', 'attachment')
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;
		$criteria->addSearchCondition('name', $this->keyword, true, 'OR');
		$criteria->addSearchCondition('file_name', $this->keyword, true, 'OR');
		$criteria->compare('id',$this->id);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('file_name',$this->file_name,true);
		$criteria->compare('create_date',$this->create_date,true);
		$criteria->compare('created_by',$this->created_by,true);
		$criteria->compare('update_date',$this->update_date,true);
		$criteria->compare('updated_by',$this->updated_by,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	public function beforeSave() {
		if (parent::beforeSave()) {
	
			$current_date = date(LGConstant::DATE_FORMAT_FOR_DATABASE);
	
			//Get user is logging
			$logedInUser = User::model()->findByPk(Yii::app()->user->id);
	
			if ($this->isNewRecord) {
				$this->create_date = $current_date;
				$this->created_by = $logedInUser->username;
			}
	
			$this->update_date = $current_date;
	
			if (isset($logedInUser)) {
				$this->updated_by = $logedInUser->username;
			}
	
			$this->setIsNewRecord(false);
			return true;
		} else {
			return false;
		}
	}
	
	public function loadAttachmentsByProfile($id){
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.
		$criteria=new CDbCriteria;
		$criteria->compare('profile_id',$id,true);
		
		return new CActiveDataProvider($this, array(
				'criteria'=>$criteria,
		));
	}
}